local tishen = fk.CreateSkill {

  name = "joyex__tishen",

  tags = { Skill.Compulsory, },

}



tishen:addEffect(fk.CardEffectCancelledOut, {
  name = "joyex__tishen",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.CardEffectCancelledOut, fk.TargetSpecified,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardEffectCancelledOut then
      return target == player and player:hasSkill(tishen.name) and data.card.trueName == "slash" and player:getMark("@@joyex__tishen-phase") == 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified  then
      return target == player and player:hasSkill(tishen.name) and player:getMark("@@joyex__tishen-phase") > 0 and data.card.trueName == "slash"
    elseif event == fk.CardUseFinished then
      return table.contains(data:getAllTargets(), player.id) and player:hasSkill(tishen.name) and not (data.damageDealt and data.damageDealt[player.id])  and data.card and data.card.trueName == "slash" and player.room:getCardArea(data.card) == Card.Processing
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardEffectCancelledOut then
        room:setPlayerMark(player, "@@joyex__tishen-phase", 1)
    elseif event == fk.TargetSpecified then
      room:setPlayerMark(player, "@@joyex__tishen-phase", 0)
      data.disresponsive = true
      data.additionalDamage = (data.additionalDamage or 0) + 1
    elseif  event == fk.CardUseFinished then
      if not player.dead then
        player.room:obtainCard(player, data.card, true, fk.ReasonPrey)
      end
    end
  end,
})
tishen:addEffect(fk.TargetSpecified, {
  name = "joyex__tishen",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.CardEffectCancelledOut, fk.TargetSpecified,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardEffectCancelledOut then
      return target == player and player:hasSkill(tishen.name) and data.card.trueName == "slash" and player:getMark("@@joyex__tishen-phase") == 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified  then
      return target == player and player:hasSkill(tishen.name) and player:getMark("@@joyex__tishen-phase") > 0 and data.card.trueName == "slash"
    elseif event == fk.CardUseFinished then
      return table.contains(data:getAllTargets(), player.id) and player:hasSkill(tishen.name) and not (data.damageDealt and data.damageDealt[player.id])  and data.card and data.card.trueName == "slash" and player.room:getCardArea(data.card) == Card.Processing
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardEffectCancelledOut then
        room:setPlayerMark(player, "@@joyex__tishen-phase", 1)
    elseif event == fk.TargetSpecified then
      room:setPlayerMark(player, "@@joyex__tishen-phase", 0)
      data.disresponsive = true
      data.additionalDamage = (data.additionalDamage or 0) + 1
    elseif  event == fk.CardUseFinished then
      if not player.dead then
        player.room:obtainCard(player, data.card, true, fk.ReasonPrey)
      end
    end
  end,
})
tishen:addEffect(fk.CardUseFinished, {
  name = "joyex__tishen",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.CardEffectCancelledOut, fk.TargetSpecified,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardEffectCancelledOut then
      return target == player and player:hasSkill(tishen.name) and data.card.trueName == "slash" and player:getMark("@@joyex__tishen-phase") == 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified  then
      return target == player and player:hasSkill(tishen.name) and player:getMark("@@joyex__tishen-phase") > 0 and data.card.trueName == "slash"
    elseif event == fk.CardUseFinished then
      return table.contains(data:getAllTargets(), player.id) and player:hasSkill(tishen.name) and not (data.damageDealt and data.damageDealt[player.id])  and data.card and data.card.trueName == "slash" and player.room:getCardArea(data.card) == Card.Processing
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardEffectCancelledOut then
        room:setPlayerMark(player, "@@joyex__tishen-phase", 1)
    elseif event == fk.TargetSpecified then
      room:setPlayerMark(player, "@@joyex__tishen-phase", 0)
      data.disresponsive = true
      data.additionalDamage = (data.additionalDamage or 0) + 1
    elseif  event == fk.CardUseFinished then
      if not player.dead then
        player.room:obtainCard(player, data.card, true, fk.ReasonPrey)
      end
    end
  end,
})

return tishen